﻿var SimpleArticle= new Object();
SimpleArticle = function () {
    this.IntRegExp = JsConst.IntRegExp;
    this.QuantityValidationFailedVal = JsConst.QuantityValidationFailedVal;
    this.AllowedKeyValues = JsConst.AllowedKeyValues;

    this.RemoveFromBasketUrl = JsConst.RemoveFromBasketUrl;
    this.AddToBasketUrl = JsConst.AddToBasketUrl;
    this.RememberArticleUrl = JsConst.RememberArticleUrl;
    this.UpdateBasketUrl = JsConst.UpdateBasketUrl;

    this.PriceTbxObj = null;
    this.QuantityObj = null;
    this.PriceVal = null;
    this.ArticleId = null;

    this.AddtoBasketBtnObj = null

    this.Init = function (priceTbxId, quantityId, priceVal, id) {
        this.PriceTbxObj = $("#" + priceTbxId);
        this.QuantityObj = $("#" + quantityId);
        this.PriceVal = priceVal;
        this.ArticleId = id;
    }


    this.IsQuantitySet = function () {
        var quantityStr = this.QuantityObj.val() + "";

        return quantityStr != "" && quantityStr != this.LocalizedNumberDecimalSeparator;
    }
    /********************** EVENTS ***********/
    this.NumbersOnly = function (e) {
        var key;
        var keychar;

        if (window.event) {
            key = window.event.keyCode;
        }
        else if (e) {
            key = e.which;
        }
        else {
            return true;
        }
        keychar = String.fromCharCode(key);

        if ((key == null) || (key == 0) || (key == 8) || (key == 9) || (key == 13) || (key == 27)) {
            return true;
        }
        else if (((this.AllowedKeyValues).indexOf(keychar) > -1)) {
            return true;
        }
        else
            return false;
    }

    this.SetPrice = function (articlePrice) {
        var price = articlePrice.PriceVal;

        if (this.IsQuantitySet()) {
            this.ValidateAndFixInputQuantity();

            price = price * this.QuantityObj.val();
        }

        this.PriceTbxObj.text(price);
    }
    this.ValidateAndFixInputQuantity = function () {
        if (this.IsQuantitySet()) {
            var val = this.QuantityObj.val();


            if (this.IntRegExp.test(val) == false) {
                var val = parseInt(val);
                if (isNaN(val)) val = this.QuantityValidationFailedVal;

                this.QuantityObj.val(val);
            }

        }
    }
    this.QuantityChange = function () {
        this.SetPrice(this);
    }
    /*--************* ADD ARTICLE TO CART ************************-- */

    this.AddToBasket = function (button) {
        this.AddtoBasketBtnObj = $(button);

        if (this.IsQuantitySet()) {
            var quantity = this.QuantityObj.val();

            if (quantity > 0) {
                var articlePrice = this;
                if (articlePrice != null) {
                    /*this.BlockAddToBasket(true);*/
                    this.SendJSON(articlePrice, quantity, this.AddToBasketUrl, this.CallBack_AddToBasket);
                }
                else {
                    alert(JsConst.ErrorMsg_PriceList_AddToCart_ArticlePriceNotFound);
                }
            }
            else {
                alert(JsConst.ErrorMsg_PriceList_AddToCart_ZeroQuantity);
            }
        }
        else {
            alert(JsConst.ErrorMsg_PriceList_AddToCart_NoQuantity);
        }
    }
    this.BlockAddToBasket = function (val) {
        if (this.AddtoBasketBtnObj != null) {
            if (val == true) {
                this.AddtoBasketBtnObj.attr('disabled', true);
            }
            else {
                this.AddtoBasketBtnObj.attr('disabled', false);
            }
        }
    }

    this.GetArticleInputModel = function (articlePrice, quantity) {
        /* 
        Here is the Mapping from JavaScript ArticlePrice to ServerSide ArticleInputModel class. 
               
        TODO: Check the ServerSide Quantity NumberDecimalSeparator. 
        If it's also Localized, replace the quantity JsNumberDecimalSeparator by LocalizedNumberDecimalSeparator.
        */
        return (articlePrice == null) ? null : { Id: articlePrice.ArticleId, Quantity: quantity };
    }

    this.CallBack_AddToBasket = function (callBackData, self) {
        /*self.BlockAddToBasket(false);*/

        if (typeof (callBackData) == "object") {
            /*
            $("#dialog-AddToBasket-content")
            .html("<b>Received server-response:</b>:"
            + "<br/> BasketArticleId:" + callBackData.BasketArticleId
            + "<br/> ArticleId:" + callBackData.ArticleId
            + "<br/> SizeName:" + callBackData.SizeName
            + "<br/> Quantity:" + callBackData.Quantity
            );                    
            */

            //Dialog.ShowAddToBasket();

            if (typeof (Basket) == "object") Basket.Refresh();
        }
        else {
            alert(JsConst.ErrorMsg_PriceList_CallBack_AddToCart_NotAnObject + "\n" + callBackData);
        }
    }

    /*--************* SEND JSON ************************-- */
    this.SendJSON = function (articlePrice, quantity, url, callBack) {
        if (url + '' == '') {
            alert(JsConst.ErrorMsg_PriceList_SendJSON_UrlEmpty);
            return;
        }
        /* Map the js object to serverside class representation */
        var article = this.GetArticleInputModel(articlePrice, quantity);

        if (article == null) {
            alert(JsConst.ErrorMsg_PriceList_SendJSON_ArticleObjectNull);
            return;
        }

        //var json = $.toJSON(article);
        var json = JSON.stringify(article);
        var self = this;

        $.ajax({
            url: url,
            cache: false,
            type: 'POST',
            dataType: 'json',
            data: json,
            contentType: 'application/json; charset=utf-8',
            success: function (data) {
                /* get the result and do some magic with it*/
                callBack(data, self);
            },
            error: function (xhr, ajaxOptions, thrownError) {
                callBack(xhr.status + ":" + thrownError, self);
            }
        });
    }

    /*--************* ***********************-- */

}